local lower = 
{
  '', '', '', '', '', '', '', '', '', '',
  '', '', '', '', '', '', '1', '2', '3', '4',
  '5', '6', '7', '8', '9', '0', '-', '=', '', '',
  'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',
  '[', ']', '\\', '', 'a', 's', 'd', 'f', 'g', 'h',
  'j', 'k', 'l', ';', '\'', '', '', 'z', 'x', 'c',
  'v', 'b', 'n', 'm', ',', '.', '/', '', '', '',
  '', ' ', '', '', '', '', '', '', '', '',
  '', '', '', '', '', '', '', '', '', '',
  '/', '*', '-', '', '', '', '', '', '', '',
  '', '', '', '', '+', '', '~'
}

local upper =
{
  '', '', '', '', '', '', '', '', '', '',
  '', '', '', '', '', '', '!', '@', '#', '$',
  '%', '^', '&', '*', '(', ')', '_', '+', '', '',
  'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P',
  '{', '}', '|', '', 'A', 'S', 'D', 'F', 'G', 'H',
  'J', 'K', 'L', ':', '"', '', '', 'Z', 'X', 'C',
  'V', 'B', 'N', 'M', '<', '>', '?', '', '', '',
  '', ' ', '', '', '', '', '', '', '', '',
  '', '', '', '', '', '', '', '', '', '',
  '/', '*', '-', '1', '2', '3', '4', '5', '6', '7',
  '8', '9', '0', '.', '+', '', '`'
}

if keyboard then
  -- converts keyboard key code to ansi character
  function keyboard:get_char(key)
    local nlock = keyboard:is_toggled(KEY_NUMLOCK)
    local clock = keyboard:is_toggled(KEY_CAPS)
    local shift = keyboard:is_down(KEY_LSHIFT) or keyboard:is_down(KEY_RSHIFT)

    local up = false
    if key >= KEY_NUMLOCK and key <= KEY_NUMENTER then
      up = nlock == true
    else
      up = shift == true
    end

    local char = lower[key]
    if up == true then
      char = upper[key]
    end
    -- apply caps lock
    if clock == true then
      if shift == true then
        char = string.lower(char)
      else
        char = string.upper(char)
      end
    end
    return char
  end
end

-- npad to non-npad
local npad =
{
  KEY_NUMDEL = KEY_DEL,
  KEY_NUM_2 = KEY_DOWN,
  KEY_NUM_4 = KEY_LEFT,
  KEY_NUM_6 = KEY_RIGHT,
  KEY_NUM_8 = KEY_UP,
  KEY_NUM_7 = KEY_HOME,
  KEY_NUM_1 = KEY_END,
  KEY_NUM_3 = KEY_PGDOWN,
  KEY_NUM_9 = KEY_PGUP,
  KEY_NUMENTER = KEY_ENTER
} 

keyboard.get_npad = function(keyboard, key)
  return npad[key]
end